<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<view class="tree">
			<view class="tree-th main-flex">
				<view class="tree-td" style="flex: 1;">人员信息</view>
				<view class="tree-td" style="flex: 1;">部门岗位</view>
				<view class="tree-td" style="width: 120rpx; text-align: center;">查看</view>
			</view>
			<ty-tree
				:data="tableList"
				childrenField= 'children'
				labelField='parentUserNick'
				:defaultExpandAll="true"
				keyField='userId'>
				<template v-slot:render-label="{data}">
					<view class="tree-tr main-flex">
						<view class="tree-td" style="flex: 1;">
							<view class="tree-td-cell main-flex">
								姓名：
								<view class="tree-td-text">{{data.originNode.nickName}}</view>
							</view>
							<view class="tree-td-cell main-flex">
								上级：
								<view class="tree-td-text">{{data.originNode.parentUserNick}}</view>
							</view>
							<view class="tree-td-cell main-flex">
								手机：
								<view class="tree-td-text">{{data.originNode.phonenumber}}</view>
							</view>
							<view class="tree-td-cell main-flex">
								角色：
								<view class="tree-td-text">{{data.originNode.roleName}}</view>
							</view>
						</view>
						<view class="tree-td" style="flex: 1;">
							<view class="tree-td-cell main-flex">
								部门：
								<view class="tree-td-text">{{data.originNode.deptName}}</view>
							</view>
							<view class="tree-td-cell main-flex">
								岗位：
								<view class="tree-td-text">{{data.originNode.postName}}</view>
							</view>
							<view class="tree-td-cell main-flex">
								职级：
								<view class="tree-td-text">{{data.originNode.level}}</view>
							</view>
							<view class="tree-td-cell main-flex">
								团队层级：
								<view class="tree-td-text">{{data.originNode.sn}}</view>
							</view>
						</view>
						<view class="tree-td" style="width: 120rpx; text-align: center;">
							<button v-if="checkPermi(['zhouyz:kpiPostSchedule:list'])" @click="tab.navigateTo(`/pages/kpi/kpiPostSchedule?userId=${data.originNode.userId}`)" class="tree-td-btn main-flex" size="mini" type="primary">KPI达成</button>
							<button v-if="checkPermi(['zhouyz:salesmanPath:list'])" @click="tab.navigateTo(`/pages/sale/salesmanPath?userId=${data.originNode.userId}`)" class="tree-td-btn main-flex" size="mini" type="primary">销售路线</button>
							<button v-if="checkPermi(['zhouyz:salesmanClue:list'])" @click="tab.navigateTo(`/pages/sale/salesmanClue?userId=${data.originNode.userId}`)" class="tree-td-btn main-flex" size="mini" type="primary">销售拜访</button>
							<!-- <button v-if="checkPermi(['zhouyz:salesmanClueFollowProduct:list'])" @click="tab.navigateTo(`/pages/sale/salesmanClueFollowProduct?userId=${data.originNode.userId}`)" class="tree-td-btn main-flex" size="mini" type="primary">试品明细</button> -->
						</view>
					</view>
				</template>
			</ty-tree>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref, getCurrentInstance } from 'vue'
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { subordinate } from "@/api/erp_api/kpi"
import { isObjectEmptySimple, copyText, arrText, handleTree } from '@/utils/ty.js'
import useUserStore from '@/store/modules/user'

	const userStore = useUserStore()
	const { proxy } = getCurrentInstance()
	// 标题设置
	const title = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		uni.setNavigationBarTitle({
			title: title.value 
		})
	})
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(1000)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		subordinate(params).then(res => {
			tableList.value = handleTree(res.data, 'userId', 'parentUserId') || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 点击行
	const rowClick = (row) => {
		
	}
 	
	onMounted(() => {
		searchFirst()
	})
</script>

<style scoped lang="scss">
</style>
